package com.bjpowernode.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONUtil;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TActivityRemark;
import com.bjpowernode.entity.TClueRemark;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.mapper.TClueRemarkMapper;
import com.bjpowernode.query.ClueRemarkQuery;
import com.bjpowernode.service.TClueRemarkService;
import com.bjpowernode.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class TClueRemarkServiceImpl implements TClueRemarkService {

    @Resource
    private TClueRemarkMapper tClueRemarkMapper;

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public int saveRemark(ClueRemarkQuery clueRemarkQuery) {
        // 创建对象
        TClueRemark tClueRemark = new TClueRemark();
        // 替换
        BeanUtils.copyProperties(clueRemarkQuery, tClueRemark);
        // 设置创建时间
        tClueRemark.setCreateTime(DateTime.now());
        // 查找创建人
        String parseToken = jwtUtil.parseToken(clueRemarkQuery.getJwt());
        // json 转 bean
        TUser tUser = JSONUtil.toBean(parseToken, TUser.class);
        tClueRemark.setCreateBy(tUser.getId());
        // 设置编辑时间
        tClueRemark.setEditTime(DateTime.now());
        // 设置编辑人
        tClueRemark.setEditBy(tUser.getId());
        return tClueRemarkMapper.insertSelective(tClueRemark);
    }

    @Override
    public PageInfo<TClueRemark> getActivityByPage(Integer clueId, Integer current) {
        // 开启分页
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 查询
        List<TClueRemark> tClueRemarkList = tClueRemarkMapper.getClueRemarkByPage(clueId);
        // 封装
        PageInfo<TClueRemark> pageInfo = new PageInfo<>(tClueRemarkList);
        return pageInfo;

    }
}
